#include "head.h"


void do_stuff(int sockfd, MSG *pbuf);
void do_admin(int sockfd, MSG *pbuf);
void show_began(MSG *pbuf);

//用户登录
void do_change(int sockfd, MSG *pbuf)
{
	char ed;
	char getstr[20];
	if(pbuf->type == 101)
	{
		printf("你是否要修改其他用户信息？(Y/N)\n");
		ed = getchar();
		if(ed == 'Y')
		{	
			printf("请输入新的id:\n");
			scanf("%d",&(pbuf->info.id));
			
			printf("请输入新的员工名:\n");
			scanf("%s",pbuf->info.name);
			
			printf("请输入新的地址:\n");
			scanf("%s",pbuf->info.addr);
			
			printf("请输入新的年龄:\n");
			scanf("%d",&(pbuf->info.age));
			
			printf("请输入新的工资:\n");
			scanf("%f",&(pbuf->info.salary));
			printf("请输入新的密码:\n");
			scanf("%s",pbuf->passwd);
			
			pbuf->type = ADMIN_CHANGE_INFO;
		}
	}	
	else if(pbuf->type == 100 || ed == 'N')
	{		
		while(scanf("%s",getstr) != 0)
		{
			printf("出错！！请重新输入。");
			while((ed = getchar()) != '\n' || ed != EOF);
		}
		strcpy(pbuf->passwd,getstr);
		pbuf->type = STAFF_CHANGE_PASSWD;
	}
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("change return:%s\n",pbuf->data);
}
void do_show(int sockfd, MSG *pbuf)
{
	if(pbuf->type == 100)
	{
		pbuf->type = STAFF_QUERY;
	}
	else 
	{
		printf("请输入你要查询的用户：\n");
		scanf("%d",&pbuf->info.id);
		pbuf->type =  ADMIN_QUERY;
	}
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	if(pbuf->ret == 0)
		printf("%s\n",pbuf->data);
	else
	{
		printf("id = %d,name = %s\naddr = %s,age = %d,salary = %f\n",pbuf->info.id,
			pbuf->info.name,pbuf->info.addr,pbuf->info.age,
			pbuf->info.salary);
	}
	return;
}

void do_delete(int sockfd, MSG *pbuf)
{
	int id;
	printf("请输入要删除的用户的id：\n");
	while(scanf("%d",&id) < 0)
	{
		printf("请输入正确的id:\n");
	}
	pbuf->info.id = id;
	pbuf->type = ADMIN_DEL_STAFF;
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("%s\n",pbuf->data);
}
void do_add(int sockfd, MSG *pbuf)
{
	char name[10];
	
			
	printf("请输入id:\n");
	scanf("%d",&(pbuf->info.id));
			
	printf("请输入员工名:\n");
	scanf("%s",pbuf->info.name);
			
	printf("请输入地址:\n");
	scanf("%s",pbuf->info.addr);
			
	printf("请输入年龄:\n");
	scanf("%d",&(pbuf->info.age));
			
	printf("请输入工资:\n");
	scanf("%f",&(pbuf->info.salary));
	
	printf("请输入密码:\n");
	scanf("&s",pbuf->passwd);
			
	pbuf->type = ADMIN_ADD_STAFF;
	send(sockfd,pbuf,sizeof(MSG),0);
	recv(sockfd,pbuf,sizeof(MSG),0);
	printf("%s\n",pbuf->data);
}

int do_login(int sockfd, MSG *pbuf)
{
	char ed;
	int ret = -1;
	int level;
	//在成功登陆前无限循环
	printf("began login\n");
	while(!(ret == 1 || ret == 0) ){
		//从终端接受用户名和密码
	        printf("name:");
		fflush(stdout);
		scanf("%d",&pbuf->info.id);
		printf("passwd:");
		fflush(stdout);
		scanf("%s",pbuf->passwd);
		//设置信息类型为 LOGIN
		pbuf->type = LOGIN;
		//发送信息
		send(sockfd,pbuf,sizeof(MSG),0);
		printf("the login return:%d,%s\n",pbuf->ret,pbuf->data);
		//接受信息
		recv(sockfd,pbuf,sizeof(MSG),0);
		ret = pbuf->ret;
		printf("the login return:%d,%s\n",pbuf->ret,pbuf->data);
		while((ed = getchar()) != '\n' && ed != EOF);
	}
	level = pbuf->type;
	if(level == 101)
		do_admin(sockfd,pbuf);
	else if(level == 100)
		do_stuff(sockfd,pbuf);
		
	return 0;
}

void do_stuff(int sockfd, MSG *pbuf)
{
	char s;
	show_began(pbuf);
	while(1)
	{
		printf("bagan use:                         \n");
		fflush(stdout);
		s = getchar();
		switch(s){
			case 'Q': return;break;
			case 'S': do_show(sockfd,pbuf);break;
			case 'C': do_change(sockfd,pbuf);break;
			default : printf("请按要求输入！！！");break;
		}
		while((s = getchar()) != '\n' && s != EOF);
		
	}
}

void do_admin(int sockfd, MSG *pbuf)
{ 
	char s;
	MSG this_msg = *pbuf;
	show_began(&this_msg);
	printf("do_admin show end \n");
	while(1)
	{
		printf("bagan use:                         \n");
		fflush(stdout);
		s = getchar();
		switch(s){
			case 'Q': return;break;
			case 'S': pbuf->type = 101; do_show(sockfd,pbuf);break;
			case 'C': do_change(sockfd,pbuf);break;
			case 'D': do_delete(sockfd,pbuf);break;
			case 'A': do_add(sockfd,pbuf);break;
			default : printf("请按要求输入！！！");break;
		}
		while((s = getchar()) != '\n' && s != EOF);
		
	}

    
}
void show_began(MSG *pbuf)
{
	char level[16] = {'\0'};
	if(pbuf->type == 101)
		strcpy(level,"管理员");
	else
		strcpy(level,"员工");
	printf("********欢迎使用员工管理系统********\n");
	printf("***                              ***\n");
	printf("user:%10s       登陆模式:%4s     \n",pbuf->name,level);      
    printf("***                              ***\n");
	printf("***  Q  退出                     ***\n");
	printf("***  S  显示员工信息             ***\n");
	if(pbuf->type == 100)
	{
		printf("***  C  修改密码                 ***\n");
	}else if(pbuf->type == 101)
	{
		printf("***  C  修改员工信息       ***\n");
		printf("***  D  删除员工信息             ***\n");
		printf("***  A  增加员工信息             ***\n");
	}
	printf("show end \n");
	return;
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    bzero(&buf,sizeof(MSG));
    int longth_addr = sizeof(struct sockaddr);
    char clean[64];
	
    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
    //连接服务器
	if((ret = connect(sockfd,(struct sockaddr *)&servaddr,longth_addr)) != 0)
	{
	    printf("connect server error!!\n");
		return -1;
	}
	do_login(sockfd,&buf);
	
	close(sockfd);
	return 0;
}
